import {
  getMaterialList,
} from '@/api/process/mesProcessMaterial';
import { getDict } from '@/api/common';
import { uniqueKey } from '@/utils/judge';

export const materialDialog = {
  data() {
    return {
      materialDialogVisible: false,  // 表格弹框隐藏显示
      getMaterialList,  // 获取弹框数据接口
      materialSearchConfig: {
        inline: true,
        formDesc: {
          goodsCode: {
            type: 'input',
            layout: 8,
            label: $i18n.t('mesProcessMaterial.goodsCode')
          },
          goodsName: {
            type: 'input',
            layout: 8,
            label: $i18n.t('mesProcessMaterial.goodsName')
          },
          goodsTypeKey: {
            type: 'select',
            label: $i18n.t('mesProcessMaterial.goodsTypeKey'),
            prop: {
              value: 'value',
              text: 'description'
            },
            options: async (data) => {
              const list = await getDict('material_type')
              return list.data.result
            }
          }
        }
      },
      materialColumn: [
        {
          type: 'selection',
          fixed: 'left'
        },
        {
          type: 'index',
          label: $i18n.t('common.order'),
          width: 60,
          fixed: 'left'
        },
        {
          prop: 'goodsCode',
          label: $i18n.t('mesProcessMaterial.goodsCode'),
          width: '160',
          showOverflowTooltip: true,
          fixed: 'left'
        },
        {
          prop: 'goodsName',
          label: $i18n.t('mesProcessMaterial.goodsName'),
          width: '200',
          showOverflowTooltip: true
        },
        {
          prop: 'goodsDesc',
          label: $i18n.t('mesProcessMaterial.goodsDesc'),
          width: '160',
          showOverflowTooltip: true
        },
        {
          prop: 'seriesName',
          label: $i18n.t('mesProcessMaterial.seriesCode'),
          width: '160',
          showOverflowTooltip: true,
        },
        {
          prop: 'goodsTypeKey',
          label: $i18n.t('mesProcessMaterial.goodsTypeKey'),
          width: '140',
          showOverflowTooltip: true,
          optionProp: {
            value: 'value',
            label: 'description'
          },
          option: async (data) => {
            const list = await getDict('material_type')
            self.goodsTypeList = list.data.result
            return list.data.result
          }
        },
        {
          prop: 'manufactureMethodValue',
          label: $i18n.t('mesProcessMaterial.manufactureMethodValue'),
          width: '180',
          showOverflowTooltip: true,
          optionProp: {
            value: 'value',
            label: 'description'
          },
          option: async (data) => {
            const list = await getDict('manufacture_method');
            self.manufactureMethodList = list.data.result;
            return list.data.result;
          }
        },
        {
          prop: 'unit',
          label: $i18n.t('common.unit'),
          showOverflowTooltip: true
        },
        {
          prop: 'specification',
          label: $i18n.t('mesProcessMaterial.specification'),
          width: '160',
          showOverflowTooltip: true
        },
        {
          prop: 'gbCode',
          label: $i18n.t('mesProcessMaterial.gbCode'),
          width: '180',
          showOverflowTooltip: true
        }
      ],
      materialDialogFooter: [  // 物料查询dialog底部按钮
      {
        name: $i18n.t("common.cancel"),
      },
      {
        name: $i18n.t("common.ok"),
        type: 'primary',
        disabled: (data, otherParam) => {
          if (otherParam['isMultiple']) {
            return false;
          } else {
            return data.length !== 1;
          }
        }
      },
    ],
    }
  },
  methods: {
    // 弹框表格，双击表格，或者点击确定 返回选中数据
    getSelectedMaterial(data) {
      // 这里可以自定义返回字段信息
      // 第一个入参为当前表单绑定的对象，第二个入参为要修改的字段名，第三个入参为选中数据中相应信息
      // 有三个位置用到物料列表，1：类型为产品时，产品选择框，2：新增时，物料列表，3：编辑时物料编码
      if (this.currentField === 'product') {  // 如果是产品，只能单选，返回的是数组，只取第一个
        console.log(data, 'daa')
        this.$set(this.formDataDialog, 'prodName', data[0]['goodsName']);
        this.$set(this.formDataDialog, 'prodCode', data[0]['goodsCode']);
        this.$set(this.formDataDialog, 'goodsId', data[0]['id']);
        this.$set(this.formDataDialog, 'goodsCode', data[0]['goodsCode']);
        this.$set(this.formDataDialog, 'prodSpecification', data[0]['specification']);
        // 表单上不显示这2个字段，但是新增或者修改时需要传这2个字段
        this.otherData = {
          seriesCode: data[0]['seriesCode'],
          seriesName: data[0]['seriesName']
        }
      } else if (this.currentField === 'materialCode' && this.dialogStatus === 'edit') {
        this.$set(this.formDataDialog, 'materialCode', data[0]['goodsCode']);
        this.$set(this.formDataDialog, 'materialId', data[0]['id']);
        this.$set(this.formDataDialog, 'materialName', data[0]['goodsName']);
        this.$set(this.formDataDialog, 'specification', data[0]['specification']);
      } else {
        data.forEach(item => {
          this.formDataDialog.materialList.push({
            materialCode: item['goodsCode'],
            materialName: item['goodsName'],
            materialSpecification: item['specification'],
            isKey: item['isKey'] || '0',
            isRepeat: item['isRepeat'],
          });
        })
        this.formDataDialog.materialList = uniqueKey(this.formDataDialog.materialList, 'materialCode');
      }
    },
    // 物料查询dialog底部取消和确定按钮事件
    changeMaterialDialog(data) {
      data.name === $i18n.t('common.ok') && this.getSelectedMaterial(data.rows);
      this.materialDialogVisible = false;
    },
  }
}
